const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      page:1,
      limit:20,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: options.uid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGoodsList();
  },

  //店铺商品列表
  getGoodsList: function () {
    var that = this;
    var page = that.data.page;
    var params = {
      limit: that.data.limit,
      page: that.data.page
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + "inn/getGoodsList",
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.search;
          var totalPage = paging.total_page;
          var resultList = res.data.data.goods_list;
          var inn_id = res.data.data.inn_info.inn_id;
          if (page == 1) {
            var itemData = resultList;
          } else {
            var itemData = that.data.product.concat(resultList)
          }
          that.setData({
            total_page: totalPage,
            product: itemData,
            inn_id: inn_id,
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail:function(fail){
        wx.hideLoading();
        util.showError("网络错误");
      }
    })
  },

  //页面下拉刷新
  onPullDownRefresh: function () {
    this.refresh();
  },

  //刷新数据
  refresh: function () {
    var that = this;
    that.setData({
      page: 1,
    });
    setTimeout(function () {
      that.getGoodsList()
    }, 500)
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },

  //加载更多
  loadMore: function () {
    var that = this;
    if (that.data.page >= that.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      page: that.data.page + 1
    });
    setTimeout(function () {
      that.customerListGet()
    }, 500)
  },

  //商品勾选
  selected:function(e){
    var index = e.currentTarget.dataset.index;
    var data = this.data.product;
    if (data[index].selected == 0 || data[index].selected==undefined){
      data[index].selected = 1
    }else{
      data[index].selected = 0
    }
    this.setData({
      product:data
    })
  },

  //提交
  submit:function(){
    var that=this;
    var data = that.data.product;
    var sku_code = [];
    var inn_product_id = [];
    for(var i=0;i<data.length;i++){
      if(data[i].selected==1){
        sku_code.push(data[i].sku_code)
        inn_product_id.push(data[i].id)
      }
    }

    if(sku_code.length==0){
      util.showError('请选择商品');
      return;
    }

    api.POST({
      params: {
        uid: that.data.uid,
        sku_code: sku_code,
        inn_product_id: inn_product_id
      },
      url: app.globalData.base_url + 'inn/sendGoods',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          util.showError('推送成功');
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            })
          },1000)
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误");
      }
    })
  }
})